using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Modules;
using ProjectDirector.Domain.Concretes;
using ProjectDirector.Domain.Abstracts;

namespace ProjectDirector.Web.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        // A Ninject "kernel" is the thing that can supply object instances
        private IKernel kernel = new StandardKernel(new ProjectDirectorServices());

        // ASP.NET MVC calls this to get the controller for each request
        protected override IController GetControllerInstance(RequestContext context, Type controllerType)
        {
            if (controllerType == null)
                return null;
            return (IController)kernel.Get(controllerType);
        }

        // Configures how abstract service types are mapped to concrete implementations
        private class ProjectDirectorServices : NinjectModule
        {
            string connection = ConfigurationManager.ConnectionStrings["ProjectDirectorConnectionString"].ConnectionString;
            public override void Load()
            {
                Bind<IProjectDirectorRepository>()
                    .To<ProjectDirectorRepository>()
                    .WithConstructorArgument("connection",connection);
                
            }
        }
    }
}